package cn.cscb.mms.server.controller;

import cn.cscb.mms.common.result.Result;
import cn.cscb.mms.common.utils.JwtUtil;
import cn.cscb.mms.pojo.dto.EmployeeLoginDTO;
import cn.cscb.mms.pojo.vo.EmployeeLoginVO;
import cn.cscb.mms.server.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {


    @Resource
    private LoginService loginService;

    @Resource
    private JwtUtil jwtUtil;

    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        EmployeeLoginVO vo = loginService.login(employeeLoginDTO);
        Map<Object, Object> payload = new HashMap<>();
        payload.put("empNo", vo.getEmpNo());
        String jwt = jwtUtil.createJwt(payload);
        vo.setToken(jwt);
        return Result.success(vo);
    }
}
